import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/4 15:01
 * @description 模拟优化，增量另用数组保存，pop时候才使用
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    class CustomStack {
        int[] stack;
        int[] add;
        int top;

        public CustomStack(int maxSize) {
            stack = new int[maxSize];
            add = new int[maxSize];
            top = -1;
        }

        public void push(int x) {
            if (top != stack.length - 1) {
                ++top;
                stack[top] = x;
            }
        }

        public int pop() {
            if (top == -1) {
                return -1;
            }
            // 弹出数据的时候，将本来的值和增量相加，得到最终结果
            int ret = stack[top] + add[top];
            if (top != 0) {
                // 传递增量到底部
                add[top - 1] += add[top];
            }
            // 增量使用后置0
            add[top] = 0;
            --top;
            return ret;
        }

        public void increment(int k, int val) {
            int limit = Math.min(k - 1, top);
            if (limit >= 0) {
                add[limit] += val;
            }
        }
    }
}

